package cn.fangline.camerademo02;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.otaliastudios.cameraview.AspectRatio;
import com.otaliastudios.cameraview.CameraUtils;

import java.io.File;
import java.lang.ref.WeakReference;

/**
 * Created by 抽离 on 2018/11/6.
 */
public class PreviewActivity extends Activity {

	private static WeakReference<byte[]> image;

	public static void setImage(@Nullable byte[] im) {
		image = im != null ? new WeakReference<>(im) : null;
	}

	private ImageView previewImage;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_preview);
		previewImage = findViewById(R.id.previewImage);

		final long delay = getIntent().getLongExtra("delay", 0);
		final int nativeWidth = getIntent().getIntExtra("nativeWidth", 0);
		final int nativeHeight = getIntent().getIntExtra("nativeHeight", 0);
		byte[] b = image == null ? null : image.get();
		if (b == null) {
			finish();
			return;
		}


		CameraUtils.decodeBitmap(b, 5000, 5000, new CameraUtils.BitmapCallback() {
			@Override
			public void onBitmapReady(Bitmap bitmap) {
				previewImage.setImageBitmap(bitmap);
				FileUtils.saveBitmap(PreviewActivity.this, bitmap);
			}
		});
	}
}
